﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace QD.Snapshot.Core.DAL
{
    public class PromotionReport : DAO
    {
        static string tableName = "sql_s003.dbo.vIFO_PromotionReport";

        public static int Add(Model.Snap model)
        {
            string sql = string.Format("insert into {0} (userid,ItemID,Keyword,PicUrl,Remarks,Ranking,Url,CreateItem,UpdateTime,Page,CopyFrom,fileName,DataZone,Infourl,htmlurl) values (@userid,@ItemID,@Keyword,@PicUrl,@Remarks,@Ranking,@Url,@CreateItem,@UpdateTime,@Page,@CopyFrom,@fileName,@DataZone,@Infourl,@htmlurl)", tableName);

            SqlParameter[] param = {  new SqlParameter("@userid",model.userid),
                                       new SqlParameter("@ItemID",model.itemid),
                                       new SqlParameter("@Keyword",model.keyword),
                                       new SqlParameter("@PicUrl",""),
                                       new SqlParameter("@Remarks",model.ranking),
                                       new SqlParameter("@Ranking",model.ranking),
                                       new SqlParameter("@Url",model.clientid),
                                       new SqlParameter("@CreateItem",model.updatetime),
                                       new SqlParameter("@UpdateTime",model.updatetime),
                                       new SqlParameter("@Page",model.page),
                                       new SqlParameter("@CopyFrom",model.copyform),
                                       new SqlParameter("@fileName",""),
                                       new SqlParameter("@DataZone",model.platform),
                                       new SqlParameter("@Infourl",model.index_key),
                                       new SqlParameter("@htmlurl",model.picurl)
                                   };
            return SqlHelper2013.ExecuteNonQuery(sql, param);
        }

        public static int UpdateHtmlUrl(Model.Snap model, int id)
        {
            string sql = string.Format("update {0} set updatetime='{1}',ranking={2},page={3},htmlurl='{4}',datazone={5},url='{6}' where id={7}"
                , tableName, model.updatetime, model.ranking, model.page, model.picurl, model.platform,model.clientid, id);

            return SqlHelper2013.ExecuteNonQuery(sql, null);
        }


        public static int GetId(Model.Snap model)
        {
            List<Model.Snap> list = GetList(model.itemid, model.keyword, model.page, model.copyform);
            if (list == null)
                return -1;

            int index = -1;
            for (int i = 0; i < list.Count; i++)
            {
                //上一次的行索引
                if (list[i].ranking == model.ranking)
                    index = i;

                //上词平台
                if ((!string.IsNullOrEmpty(model.platform)) && list[i].platform.Equals(model.platform))
                {
                    index = i;
                }

                //校验上一次上词第几行的标识(标识优先级最高)
                if (list[i].index_key.Equals(model.index_key))
                {
                    index = i;
                    break;
                }
            }

            if (index > -1 && list.Count > 0)
                return list[index].id;
            else
                return 0;
        }

        public static List<Model.Snap> GetList(int itemid, string keyword, int page, int copyfrom)
        {
            List<Model.Snap> list = new List<Model.Snap>();
            string sql = string.Format("select top 50 id,ranking,DataZone,infourl from {0} where itemid={1} and keyword='{2}' and page={3} and copyfrom={4}", tableName, itemid, keyword, page, getCopyform(copyfrom));
            try
            {
                using (SqlDataReader dr = SqlHelper2013.ExecuteReader(CommandType.Text, sql, null))
                {
                    if (dr != null)
                    {
                        if (dr.HasRows) list = new List<Model.Snap>();

                        while (dr.Read())
                        {
                            //行标识判断
                            Model.Snap model = new Model.Snap()
                            {
                                id = Convert.ToInt32(dr["id"]),
                                ranking = Convert.ToInt32(dr["ranking"]),
                                platform = Convert.ToString(dr["DataZone"]),
                                index_key = Convert.ToString(dr["infourl"])
                            };
                            list.Add(model);
                        }
                        dr.Close();
                    }
                }
            }
            catch { }
            return list;
        }

        static string getCopyform(int copyform)
        {
            //if (copyform == 0)
            //    return "0";
            //else if (copyform == 1)
            //    return "360";
            //else if (copyform == 2)
            //    return "搜狗";
            //else if (copyform == 3)
            //    return "Bing";
            //else if (copyform == 4)
            //    return "中国搜索";
            //else if (copyform == 5)
            //    return "微信";
            return copyform.ToString();
        }


        /// <summary>
        /// 更新监控平台
        /// </summary>
        /// <param name="copyform"></param>
        /// <param name="platform"></param>
        /// <returns></returns>
        public static int UpdatePlatform(string copyform, string platform) {
            string sql = string.Format("update sql_s003.dbo.vIFO_Report_Snap_Platform set SearchEngine=SearchEngine+1 where Copyfrom=@Copyfrom and Platform=@Platform and datediff(dd,SnapDate,getdate())=0 ");
            SqlParameter[] param = {  new SqlParameter("@Copyfrom", copyform),
                                       new SqlParameter("@Platform", platform)
                                   };
            return SqlHelper2013.ExecuteNonQuery(sql, param);
        }

        /// <summary>
        /// 添加监控平台
        /// </summary>
        /// <param name="copyform"></param>
        /// <param name="platform"></param>
        /// <returns></returns>
        public static int AddPlatform(string copyform, string platform) {
            string sql = string.Format("insert into sql_s003.dbo.vIFO_Report_Snap_Platform(CopyFrom,platform,SearchEngine,SnapDate)values(@Copyfrom,@Platform,1,getdate())");
            SqlParameter[] param = {  new SqlParameter("@Copyfrom",copyform),
                                       new SqlParameter("@Platform",platform)
                                   };
            return SqlHelper2013.ExecuteNonQuery(sql, param);
        }

        /// <summary>
        /// 获取下一个自增列的值
        /// </summary>
        /// <returns></returns>
        public static int GetIncrementID() {
            string sql = string.Format("SELECT IDENT_CURRENT('{0}') + IDENT_INCR('{0}')", tableName);
            return Convert.ToInt32(SqlHelper2013.ExecuteScalar(CommandType.Text,sql));
        }
    }
}